import { Component, OnInit,AfterViewInit,Output,Input,EventEmitter,ViewChild,ElementRef } from '@angular/core';
import { Subject,Unsubscribable } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { NavController,ModalController } from '@ionic/angular';
import { AppNotification } from '@global/service/Notification';
import { App } from '@global/service/App';
import { Http } from '@global/service/Http';
import { ComUtil } from '@global/util/ComUtil';
import { AppEnvironment } from 'src/environments/AppEnvironment';

import { BGYPMobPickupMDViewPage } from '@pages/demo/bgypmob-pickup-mdview/bgypmob-pickup-mdview.page';

import { Righttoolbar } from '@widget/bgyp/tree-exp-viewrighttoolbar-toolbar/tree-exp-viewrighttoolbar-toolbar';
import { TreeExpBar } from '@widget/bgyp/tree-exp-viewtreeexpbar-treeexpbar/tree-exp-viewtreeexpbar-treeexpbar';

@Component({
  selector: 'view-bgyptree-exp-view',
  templateUrl: './bgyptree-exp-view.page.html',
  styleUrls: ['./bgyptree-exp-view.page.scss']
})
export class BGYPTreeExpViewPage implements OnInit,AfterViewInit {

    /**
     * 子组件Righttoolbar
     *
     * @public
     * @type {Subject}
     * @memberof BGYPTreeExpView
     */
    @ViewChild(Righttoolbar)
    private righttoolbar: Righttoolbar;

    /**
     * 子组件TreeExpBar
     *
     * @public
     * @type {Subject}
     * @memberof BGYPTreeExpView
     */
    @ViewChild(TreeExpBar)
    private treeexpbar: TreeExpBar;

    /**
     * 视图订阅对象
     *
     * @public
     * @type {Subject}
     * @memberof BGYPTreeExpView
     */
    public viewState: Subject<ViewState> = new Subject();

    /**
     * 路由传递参数
     *
     * @public
     * @type {void}
     * @memberof BGYPTreeExpView
     */
    public $route ={};

    /**
     * 当前视图激活数据
     *
     * @public
     * @type {void}
     * @memberof BGYPTreeExpView
     */
    public $activeData:Array<any> =[];

    /**
     * 是否为模态框弹出
     *
     * @memberof BGYPTreeExpView
     */
    @Input() isModalMode:boolean =false;

    /**
     * 是否嵌入分页导航视图
     *
     * @memberof TestEntity2MobPickupView
     */
    @Input() isTabexp:boolean =false;

    /**
     * 视图值变化事件
     *
     * @public
     * @type {EventEmitter}
     * @memberof BGYPTreeExpView
     */    
    @Output() public viewdataschange =new EventEmitter();

    /**
     * 视图参数
     *
     * @public
     * @type {Subject}
     * @memberof BGYPTreeExpView
     */
    @Input() public viewdata:any;

    /**
     * 父数据对象
     *
     * @protected
     * @type {*}
     * @memberof BGYPTreeExpView
     */
    protected srfparentdata: any = {};

    /**
     * 是否为内敛视图
     *
     * @public
     * @type {EventEmitter}
     * @memberof BGYPTreeExpView
     */ 
    @Input() public isembeddedView:boolean =false;

    /**
     * 视图值加载完成事件
     *
     * @public
     * @type {EventEmitter}
     * @memberof BGYPTreeExpView
     */ 
    @Output() public viewload =new EventEmitter();

    /**
     * 视图的关联试图Map
     *
     * @public
     * @type {Map}
     * @memberof BGYPTreeExpView
     */
    public RefViewMap =new Map();


    /**
     * 引擎初始化
     *
     * @private
     * @memberof BGYPTreeExpView
     */
    private engineInit(): void {
    }


     /**
     * angular生命周期(在ngOnInit之后)
     *
     * @memberof BGYPTreeExpView
     */
    public ngAfterViewInit(){
        this.engineInit();
        this.viewState.next({ tag: 'treeexpbar', action: 'load', data: this.viewdata });

        this.registerRefView();
    }

        /**
    * 新建
    *
    * @param {*} [arg={}]
    * @param {*} [params]
    * @memberof BGYPTreeExpView
    */
    public New(args: any[], params?: any, $event?: any, xData?: any) {
         const _this: any = this;
        if (_this.newdata && _this.newdata instanceof Function) {
            const data: any = {};
            if (_this.srfparentdata) {
                Object.assign(data, _this.srfparentdata);
            }
            _this.newdata([{ ...data }], params, $event, xData);
        } else {
            this.$notification.error('错误,视图处理逻辑不存在，请添加!');
        }
    }

    /**
     * righttoolbar 部件 click 事件
     *
     * @param {*} [args={}]
     * @param {*} $event
     * @memberof TestEntityEditView
     */
    public righttoolbar_click($event: any, $event2?: any) {
        if (Object.is($event.tag, 'tbitem1')) {
            this.righttoolbar_tbitem1_click($event, '', $event2);
        }
    }


    /**
     * 部件逻辑事件
     *
     * @param {*} [params={}]
     * @param {*} [tag]
     * @memberof BGYPTreeExpView
     */
    public righttoolbar_tbitem1_click(params: any = {}, tag?: any, $event?: any) {
        // 参数

        // 取数
        let datas: any[] = [];
        let xData: any = null;
        const paramJO = {};
        // 界面行为
        this.New(datas, paramJO,  $event, xData);
    }
    
    /**
     * 打开新建数据视图
     *
     * @param {*} [params={}]
     * @memberof BGYPTreeExpView
     */
    public async newdata(params: any = {}) {
    }

    
    /**
     * 打开编辑数据视图
     *
     * @param {*} [params={}]
     * @memberof BGYPTreeExpView
     */
    public async opendata(params: any = {}) {
    }


    /**
     * 返回
     *
     * @memberof BGYPTreeExpView
     */
    public backView() {
        let res:any;
        if(this.isModalMode){
            if(this.$activeData && this.$activeData.length >0){
                res = { ret: 'OK', result:this.$activeData };
            }else{
                res = { ret: 'NO', result:[] };
            }
            this.$modalCtrl.dismiss(res, 'close');
        }else{
            this.$navCtrl.back();
            if(this.$activeData && this.$activeData.length >0){
                res = { ret: 'OK', result:this.$activeData };
            }else{
                res = { ret: 'NO', result:[] };
            }
            this.$app.closeCallback(res);
        }
    }

    /**
     * 嵌入视图点击更多
     *
     * @public
     * @memberof BGYPTreeExpView
     */
    public async click_more(){
        let leftopts:Array<any> =[
    ];
        let rightopts:Array<any> =[
        {text:'新建',id:'righttoolbar_tbitem1'},
    ];
        let opts:Array<any>=leftopts.concat(rightopts);
        const result: string = await this.$app.openActionSheet({items:opts});
        let _this =this;
        if(result.startsWith('righttoolbar')){
            if(_this && _this.righttoolbar_click && _this.righttoolbar_click instanceof Function){
                let firstIndex = result.indexOf('_');
                _this.righttoolbar_click({tag:result.substring(firstIndex + 1)});
            }
        }
    }

    /**
     * 点击toolbar的操作组事件
     *
     * @public
     * @memberof BGYPTreeExpView
     */
    public  async openActionSheet(data:any){

        
    }

    /**
     * 注册关联视图
     *
     * @public
     * @memberof BGYPTreeExpView
     */    
    public registerRefView(){
        this.RefViewMap.set('BGYPMobPickupMDView',BGYPMobPickupMDViewPage);
    }   

    
    /**
     * 实例化对象
     *
     * @memberof BGYPTreeExpView
     */
    constructor(public $http:Http,public $navCtrl: NavController, public $notification: AppNotification, public $activatedRoute: ActivatedRoute,public $modalCtrl:ModalController,public $app:App) {
        this.$activatedRoute.queryParams.subscribe(params => {
        this.$route ="";
        if(params){
            this.$route =JSON.stringify(params);
        }
        })
    }

    /**
     * angular生命周期(组件初始化)
     *
     * @memberof BGYPTreeExpView
     */
    public ngOnInit() {
       
    }

    /**
     * 视图抛出值变化事件
     *
     * @public
     * @memberof BGYPTreeExpView
     */
    public viewDataChanges($event:any){
        this.viewdataschange.emit($event);
    }

    /**
     * 快速搜索
     *
     * @public
     * @memberof BGYPTreeExpView
     */
    public onQuickSearchValueChange($event:any){
        this.viewState.next({tag:'treeexpbar',action:'quicksearch',data:$event.detail.value});
    }
}


